<template>
    <div class="search-form">

        <!-- 头部tab切换 -->
        <el-row type="flex" class="search-tab">
            <span v-for="(item, index) in tabs" :key="index"
            @click="handleSearchTab(item, index)"
            :class="{active: index === currentTab}">
                <i :class="item.icon"></i>{{item.name}}
            </span>
        </el-row>

        <el-form class="search-form-content" ref="form" label-width="80px">
            <el-form-item label="出发城市">
                <!-- fetch-suggestions 返回输入建议的方法 -->
                <!-- select 点击选中建议项时触发 -->
                <el-autocomplete
                v-model="departCity"
                :fetch-suggestions="queryDepartSearch"
                placeholder="请搜索出发城市"
                @select="handleDepartSelect"
                class="el-autocomplete"
                ></el-autocomplete>
            </el-form-item>
            <el-form-item label="到达城市">
                <el-autocomplete
                v-model="destCity"
                :fetch-suggestions="queryDestSearch"
                placeholder="请搜索到达城市"
                @select="handleDestSelect"
                class="el-autocomplete"
                ></el-autocomplete>
            </el-form-item>
            <el-form-item label="出发时间">
                <!-- change 用户确认选择日期时触发
                value-format="yyyy-MM-dd"
                设置日期格式为：年月日，比如：2022-12-26
                 -->
                <el-date-picker type="date" 
                placeholder="请选择日期" 
                v-model="departDate"
                value-format="yyyy-MM-dd"
                style="width: 100%;"
                @change="handleDate">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="">
                <el-button style="width:100%;" 
                type="primary" 
                icon="el-icon-search"
                @click="handleSubmit">
                    搜索
                </el-button>
            </el-form-item>
            <div class="reverse">
                <span @click="handleReverse">换</span>
            </div>
        </el-form>  
      </div>
</template>

<script>
export default {
    data(){
        return {
            tabs: [
                {icon: "iconfont icondancheng", name: "单程"},
                {icon: "iconfont iconshuangxiang", name: "往返"}
            ],
            currentTab: 0,
            departCity: '', // 出发城市
            departCode: '', // 出发城市code
            departDate: '', // 出发的日期
            destCity: '',// 到达城市
            destCode: '', // 到达城市code
        }
    },
    methods: {
        // tab切换时触发
        handleSearchTab(item, index){
            if(item.name === "往返"){
                // $confirm是elementUI框架提供的方法
                this.$confirm('目前暂不支持往返，请使用单程选票！','提示',{
                    type: 'warning', // 文本前面的图标
                    confirmButtonText: '确定', // 确定按钮文本
                    showCancelButton: false // 因此取消按钮
                })
            }
        },
        
        // 出发城市输入框获得焦点时触发
        // value 是选中的值，cb是回调函数，接收要展示的列表
        queryDepartSearch(value, cb){
            // 获取搜索关键词相关的城市列表 /airs/city
            // console.log(85,value);
            this.$axios({
                url: '/airs/city',
                method: "get", // 可省略
                params: {
                    name: value
                }
            }).then(res=>{
                // console.log(93,res);
                let data = res.data.data
                data.forEach(item=>{
                    item.value = item.name.replace('市','')
                })
                cb(data)
            })
        },

        // 目标城市输入框获得焦点时触发
        // value 是选中的值，cb是回调函数，接收要展示的列表
        queryDestSearch(value, cb){
            this.queryDepartSearch(value,cb)
        },
       
        // 出发城市下拉选择时触发
        handleDepartSelect(item) {
            // console.log(114,item);
            this.departCity = item.value
            this.departCode = item.sort
        },

        // 目标城市下拉选择时触发
        handleDestSelect(item) {
            this.destCity = item.value
            this.destCode = item.sort
        },

        // 确认选择日期时触发
        handleDate(value){
           
        },

        // 出发和目标城市切换时触发
        handleReverse(){
            // 保存出发和目标城市
            let start = this.departCity
            let startCode = this.departCode
            let end = this.destCity
            let endCode = this.destCode
            // 交换出发和目标城市
            this.departCity = end
            this.departCode = endCode
            this.destCity = start
            this.destCode = startCode
        },

        // 提交表单是触发
        handleSubmit(){
            // 表单的校验
            if(!this.departCity){
                return this.$message.warning('请选择出发城市')
            }
            if(!this.destCity){
                return this.$message.warning('请选择到达城市')
            }
            if(!this.departDate){
                return this.$message.warning('请选择出发时间')
            }
            // 保存所有搜索记录
             // arr初始值为 上一次保存的arr数组（也就是缓存起来的数组）
            //  怎么获取到缓存中的数据呢？答：通过localStorage.getItem('名字')方法获取
            // localStorage.getItem('airs')||'[]' 这种写法叫短路运算，
            // 下面这行代码表示如果localStorage.getItem有数据则进行转换并赋值给arr数组
            // 如果localStorage.getItem没有数据，则把或后面的空数组进行转换并赋值给arr数组
            const arr = JSON.parse(localStorage.getItem('airs')||'[]')
            arr.unshift({
                departCity: this.departCity,
                departCode: this.departCode,
                departDate: this.departDate,
                destCity: this.destCity,
                destCode: this.destCode,
            })
            /**
             * 缓存数据
             * localStorage.setItem本地缓存的方法，把数据保存到浏览器缓存中
             * 这个方法只能保存数字和字符串类型的数据，因此需要把数组通过JSON.stringify
             * 方法转为字符串类型的数据
             * */ 
            localStorage.setItem('airs', JSON.stringify(arr) )
            // 校验通过 跳转到机票列表页面
            this.$router.push({
                path: '/air/flights',
                query: {
                    departCity: this.departCity,
                    departCode: this.departCode,
                    departDate: this.departDate,
                    destCity: this.destCity,
                    destCode: this.destCode,
                }
            })
        }
    },
    mounted() {
       
    }
}
</script>

<style scoped lang="less">
.search-form{
    border:1px #ddd solid;
    border-top:none;
    width:360px;
    height:350px;
    box-sizing: border-box;
}

.search-tab{
  span{
    display: block;
    flex:1;
    text-align: center;
    height:48px;
    line-height: 42px;
    box-sizing: border-box;
    border-top:3px #eee solid;
    background:#eee;
  }

  .active{
    border-top-color: orange;
    background:#fff;
  }

  i{
    margin-right:5px;
    font-size: 18px;

    &:first-child{
      font-size:16px;
    }
  }
}

.search-form-content{
  padding:15px 50px 15px 15px;
  position: relative;

  .el-autocomplete{
    width: 100%;
  }
}

.reverse{
  position:absolute;
  top: 35px;
  right:15px;

  &:after,&:before{
      display: block;
      content: "";
      position: absolute;
      left:-35px;
      width:25px;
      height:1px;
      background:#ccc;
  }

  &:after{
      top:0;
    }

    &:before{
      top:60px;
    }

  span{
    display: block;
    position:absolute;
    top: 20px;
    right:0;
    font-size:12px;
    background: #999;
    color:#fff;
    width:20px;
    height:20px;
    line-height: 18px;
    text-align: center;
    border-radius: 2px;
    cursor: pointer;

    &:after,&:before{
      display: block;
      content: "";
      position: absolute;
      left:10px;
      width:1px;
      height:20px;
      background:#ccc;
    }

    &:after{
      top:-20px;
    }

    &:before{
      top:20px;
    }
  }
}
</style>
